#include "FlagAlarmResponse.h"
#include "ui_FlagAlarmResponseForm.h"
#include "SettingHelper.h"

FlagAlarmResponse::FlagAlarmResponse(QWidget *parent) : QWidget(parent),
 ui(new Ui::FlagAlarmResponse)
{
   ui->setupUi(this);
   this->setWindowFlags(Qt::FramelessWindowHint);
   tc = QTextCodec::codecForName("UTF-8");

   InitTable();
}

FlagAlarmResponse::~FlagAlarmResponse()
{
    delete ui;
}

void FlagAlarmResponse::InitTable()
{
    int i=0, j=0;
    QString headline[3]={"", tr("Flag报警名称"), tr("数值(0-100)")};
    QString item[3]={tr("未成熟细胞？"), tr("异常/异型淋巴细胞？"), tr("血红蛋白异常/干扰？")};

    tableWidget_flagAlarm = ui->tableWidget;
    tableWidget_flagAlarm->setGeometry(150,170,500,180);
    QString stylesheettableWidget_listing = //"QTableWidget{background-color:rgb(236,249,241);}";
                                            "QTableWidget{background-color:#ffffff;"
                                            "alternate-background-color: #e2f7eb;"
                                            "color: rgb(0, 0, 0);"
                                            "selection-color: rgb(255, 255, 255);"
                                            "selection-background-color: rgb(32, 131, 95);}"
                                            ;
 // tableWidget_flagAlarm->setStyleSheet(stylesheettableWidget_listing);

    tableWidget_flagAlarm->setColumnCount(3);
    tableWidget_flagAlarm->setRowCount(4);
    tableWidget_flagAlarm->setEditTriggers(QAbstractItemView::AllEditTriggers);//编辑
    tableWidget_flagAlarm->setSelectionMode(QAbstractItemView::SingleSelection); //选择
//   tableWidget_flagAlarm->setSelectionBehavior(QAbstractItemView::SelectItems);

    tableWidget_flagAlarm->verticalHeader()->setResizeMode(QHeaderView::Stretch);
    tableWidget_flagAlarm->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);


    tableWidget_flagAlarm->verticalHeader()->hide();
    tableWidget_flagAlarm->horizontalHeader()->hide();

    tableWidget_flagAlarm->setColumnWidth(0,45);
    tableWidget_flagAlarm->setColumnWidth(1,225);
    tableWidget_flagAlarm->setColumnWidth(2,225);


    for(i = 0;i<3;++i)
    {
        tableWidget_flagAlarm->setItem(0,i,new QTableWidgetItem(headline[i]));
      //  tableWidget_flagAlarm->item(0,i)->setFlags( Qt::ItemIsSelectable );
        tableWidget_flagAlarm->item(0,i)->setFlags( Qt::NoItemFlags );
        tableWidget_flagAlarm->item(0,i)->setTextColor( QColor(0, 0, 0) );
        tableWidget_flagAlarm->item(0,i)->setTextAlignment(Qt::AlignCenter);
        tableWidget_flagAlarm->item(0,i)->setBackgroundColor(QColor(102, 204, 153));
    }
    for( j=0; j<2; ++j )
    {
        for(i = 1; i<4; ++i)
        {
            if( j == 0 )
            {
                tableWidget_flagAlarm->setItem(i,j,new QTableWidgetItem(QString::number(i,4)));
            }
            else if( j == 1 )
            {
                tableWidget_flagAlarm->setItem(i,j,new QTableWidgetItem( item[i-1]));
            }
         //   tableWidget_flagAlarm->item(i,j)->setFlags( Qt::ItemIsSelectable );
            tableWidget_flagAlarm->item(i,j)->setFlags( Qt::NoItemFlags );
            tableWidget_flagAlarm->item(i,j)->setTextAlignment(Qt::AlignCenter);
            tableWidget_flagAlarm->item(i,j)->setTextColor( QColor(0, 0, 0) );
            tableWidget_flagAlarm->item(i,j)->setBackgroundColor(QColor(102, 204, 153));
        }
    }
    for( i=1; i<4; ++i )
    {
        tableWidget_flagAlarm->setItem(i,2,new QTableWidgetItem("") );
        tableWidget_flagAlarm->item(i,2)->setTextAlignment(Qt::AlignCenter);
    }
    tableWidget_flagAlarm->setAlternatingRowColors(true);//间隔显示颜色
    tableWidget_flagAlarm->setStyleSheet(stylesheettableWidget_listing);


    tableWidget_flagAlarm->item(1, 2)->setText ( QString::number(SettingHelper::getSettingHelper()->m_FlagAlarmSet.ImmatureCell));
    tableWidget_flagAlarm->item(2, 2)->setText ( QString::number(SettingHelper::getSettingHelper()->m_FlagAlarmSet.AtyLympho));
    tableWidget_flagAlarm->item(3, 2)->setText ( QString::number(SettingHelper::getSettingHelper()->m_FlagAlarmSet.HGBAbnor));

    for( i = 0; i < 3; ++i )
    {
        if( tableWidget_flagAlarm->item(1+i, 2)->text().toInt() > 100 )
        {
            tableWidget_flagAlarm->item(1+i, 2)->setTextColor( QColor(255, 0, 0) );
        }
        else
        {
            tableWidget_flagAlarm->item(1+i, 2)->setTextColor( QColor(0, 0, 0) );
        }
    }
    tableWidget_flagAlarm->clearSelection();


    //声明一个委托
    delegate = new LineEditDelegate(this);
   // QString conditions = "^\\d{3}$";
    QString conditions = "0|100|([1-9][0-9])";
    delegate->getlineeditconditions(conditions);
    tableWidget_flagAlarm->setItemDelegateForColumn(2,delegate);
}

void FlagAlarmResponse::SaveSetting()
{
    SettingHelper::getSettingHelper()->m_FlagAlarmSet.ImmatureCell = tableWidget_flagAlarm->item(1, 2)->text().toInt();
    SettingHelper::getSettingHelper()->m_FlagAlarmSet.AtyLympho =  tableWidget_flagAlarm->item(2, 2)->text().toInt();
    SettingHelper::getSettingHelper()->m_FlagAlarmSet.HGBAbnor = tableWidget_flagAlarm->item(3, 2)->text().toInt();

    // 保存flag参数
    SettingHelper::getSettingHelper()->flagAlarmWriteSetting();
}

int FlagAlarmResponse::compare_param_to_bak()
{
    int ret = 0;

    if(tableWidget_flagAlarm->item(1, 2)->text() == "")
      return 1;
    else if (tableWidget_flagAlarm->item(2, 2)->text() == "")
      return 1;
    else if (tableWidget_flagAlarm->item(3, 2)->text() == "")
      return 1;

    if( SettingHelper::getSettingHelper()->m_FlagAlarmSet.ImmatureCell != tableWidget_flagAlarm->item(1, 2)->text().toInt() )
    {
        logtemp.append(tr("修改Flag报警为：未成熟细胞？：")+QString::number(SettingHelper::getSettingHelper()->m_FlagAlarmSet.ImmatureCell)+tr("→")+tableWidget_flagAlarm->item(1, 2)->text());
        ret = 1;
    }
    if( SettingHelper::getSettingHelper()->m_FlagAlarmSet.AtyLympho != tableWidget_flagAlarm->item(2, 2)->text().toInt() )
    {
        logtemp.append(tr("修改Flag报警为：异常/异型淋巴细胞？：")+QString::number(SettingHelper::getSettingHelper()->m_FlagAlarmSet.AtyLympho)+tr("→")+tableWidget_flagAlarm->item(2, 2)->text());
        ret = 1;
    }
    if(SettingHelper::getSettingHelper()->m_FlagAlarmSet.HGBAbnor != tableWidget_flagAlarm->item(3, 2)->text().toInt() )
    {
        logtemp.append(tr("修改Flag报警为：血红蛋白异常/干扰？：")+QString::number(SettingHelper::getSettingHelper()->m_FlagAlarmSet.HGBAbnor)+tr("→")+tableWidget_flagAlarm->item(3, 2)->text());
        ret = 1;
    }

    return ret;
}

int FlagAlarmResponse::modules_change_flagAlarmSave()
{
    int i = 0;

    if( 0 == compare_param_to_bak() )
    {
        return 1;
    }

    //KMessageBox msgBox ;

    for( i = 0; i < 3; ++i )
    {
        if( tableWidget_flagAlarm->item(1+i, 2)->text().toInt() > 100 || tableWidget_flagAlarm->item(1+i, 2)->text() == "" )
        {
//            msgBox.setTipInfo(tr("输入无效，退出将无法保存，是否退出？"));
//            msgBox.setBottonText("OK", tr("是"));
//            msgBox.setBottonText("Cancel", tr("否"));
//            msgBox.showKMessageBox();
//            int states = msgBox.getStates();
//            if(states == KMessageBox::OK)
//            {
//                return 1;
//            }
//            else
//            {
//                return 0;
//            }
        }
    }

//    msgBox.setTipInfo(tr("是否保存？"));
//    msgBox.setBottonText("OK", tr("是"));
//    msgBox.setBottonText("Cancel", tr("否"));
//    msgBox.showKMessageBox();
//    int states = msgBox.getStates();
//    if(states == KMessageBox::OK)
//    {
//       // for(int i=0 ;i<logtemp.count();i++)
//        //save_journal_info(tr("修改Flag报警设置"),logtemp.at(i),0);
//       // cp_flagAlarmParam_to_bak();
//       // save_flagAlarmParam();

//    }

    return 1;
}


void FlagAlarmResponse::closeEvent(QCloseEvent *e)
{

    int ret = 0;
    logtemp.clear();
   // close_softKeyboard_handle(true);
    ret = modules_change_flagAlarmSave();
    if( ret == 1 )
    {
        SaveSetting();
    }

}
